package fi.internetix.ck;

import org.w3c.dom.Node;
import org.w3c.tidy.DOMTextImpl;

public class GenericContainerNodePrinter extends HTMLNodePrinter {

  public void processNode(Node node, StringBuilder output) {
    if (node.hasChildNodes() && !containsJustAnEmptyString(node)) {
      super.processNode(node, output);
    }
  }
  
  private boolean containsJustAnEmptyString(Node node) {
    if (node.getChildNodes().getLength() == 1) {
      Node childNode = node.getFirstChild();
      if (childNode instanceof DOMTextImpl) {
        DOMTextImpl textNode = (DOMTextImpl) childNode;
        String text = textNode.getNodeValue();
        if (text == null || text.length() == 0) {
          return true;
        }
        text = NodePrinterUtils.trim(text);
        if (text == null || text.length() == 0) {
          return true; 
        }
      }
    }
    return false;
  }

}
